package utils

import (
	"encoding/json"
	"io/ioutil"
	"os"
	"path"
)

type GlobalData struct {
	Name           string
	HomeDir        string
	Host           string
	TCPPort        int
	Version        string
	MaxPacketSize  uint32
	MaxConn        int
	WorkerPoolSize uint32
	WorkerTaskLen  uint32
	MaxChanBuffLen uint32
	LogDir         string
}

var G_Data *GlobalData

func init() {
	G_Data = &GlobalData{
		Name:           "FCSVR",
		HomeDir:        "",
		Version:        "v0.1",
		TCPPort:        8080,
		Host:           "0.0.0.0",
		MaxConn:        1,
		MaxPacketSize:  409600,
		WorkerPoolSize: 10,
		WorkerTaskLen:  10,
		MaxChanBuffLen: 10,
		LogDir:         "",
	}
	G_Data.HomeDir, _ = os.Getwd()
	G_Data.LogDir = path.Join(G_Data.HomeDir, "log")
}

func (this *GlobalData) Reload() error {
	data, err := ioutil.ReadFile("conf/config.json")
	if err != nil {
		return err
	}
	err = json.Unmarshal(data, &G_Data)
	if err != nil {
		return err
	}
	return nil
}
